package managedbean.FRM.CashInAdvance;

import entity.CRM.Customer;
import entity.CRM.SaleOrder;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.CRM.SaleOrderBeanLocal;
import session.stateless.FRM.CashInAdvanceBeanLocal;
import session.stateless.FRM.CreditAccountBeanLocal;



@ManagedBean(name="refundCashInAdvanceManagerBean")
@ViewScoped
public class RefundCashInAdvanceManagerBean implements Serializable {
    public RefundCashInAdvanceManagerBean() {}
    @EJB
    private CashInAdvanceBeanLocal cashInAdvanceBeanLocal;
    @EJB
    private CreditAccountBeanLocal creditAccountBeanLocal;
    @EJB
    private CustomerBeanLocal customerBeanLocal;
    @EJB
    private SaleOrderBeanLocal saleOrderBeanLocal;
    
    private SaleOrder selectedSaleOrder;
    private Customer selectedCustomer;
    private String statusMessage;
    
    public void initView(PhaseEvent event){
        try
        {
            if(selectedSaleOrder==null)
                selectedSaleOrder = (SaleOrder)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedSaleOrder");
            this.setSelectedCustomer(selectedSaleOrder.getCustomer());
      }
        catch (Exception ex)
        {
            ex.printStackTrace();            
        }
    }
    
    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public SaleOrder getSelectedSaleOrder() {
        return selectedSaleOrder;
    }

    public void setSelectedSaleOrder(SaleOrder selectedSaleOrder) {
        this.selectedSaleOrder = selectedSaleOrder;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public void refundCashInAdvance(ActionEvent event) throws IOException
    {
        Customer customer = selectedSaleOrder.getCustomer();
        /*cashInAdvanceBeanLocal.deleteCashInAdvance(this.getSelectedSaleOrder().getId());
        Double creditCheck= creditAccountBeanLocal.isPass_checkCreditBalance(customer.getRegistrationNumber());
        if (creditCheck >0)
        {
            statusMessage="Credit Balance has been updated to "+customerBeanLocal.findCustomerById(customer.getRegistrationNumber()).getCreditAccount().getCreditBalance();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Result: " +statusMessage, "")); 
        }
        else
        {
            statusMessage="Credit Balance has been updated to "+customerBeanLocal.findCustomerById(customer.getRegistrationNumber()).getCreditAccount().getCreditBalance();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Result: " +statusMessage, "")); 
        }*/
        
        this.setSelectedSaleOrder(saleOrderBeanLocal.findSaleOrder(selectedSaleOrder.getId()));
    }
    
}
